#ifndef NJF_RASTER_FORMATS_H
#define NJF_RASTER_FORMATS_H

#include <stdint.h>
#include <stdlib.h>

#include "raster-util.h"
#include "raster-triangles.h" /* RasterSpan */

typedef struct
{
    size_t pixel_size; /* 1-4 */
    uint32_t alpha_mask;
    uint32_t ashift;
    uint32_t red_mask;
    uint32_t rshift;
    uint32_t green_mask;
    uint32_t gshift;
    uint32_t blue_mask;
    uint32_t bshift;
    Pixel *palette; /* user-defined format only */

    int (__cdecl *LineBlit)(Raster *dest, Raster *src, unsigned int num, const RasterPos *dPos, const RasterPos *sPos, BlitMode *mode);
    int (__cdecl *LineBlitUV)(Raster *dest, Raster *src, unsigned int num, const RasterPos *dPos, const RasterPosUV *leftUV, const RasterPosUV *rightUV, BlitMode *mode);
    int (__cdecl *SpanBlit)(Raster *dest, Raster *src, int y, RasterSpan *span, BlitMode *mode);
} RasterFormat;

/* an opaque raster target. Same as XRGB32 */
#define FORMAT_TARGET_OPAQUE    0
/* a raster target with alpha channel. Same as ARGB32 */
#define FORMAT_TARGET_ALPHA     1
/* a raster with flat XRGB32 storage */
#define FORMAT_FLAT_XRGB32      0
/* a raster with flat ARGB32 storage */
#define FORMAT_FLAT_ARGB32      1
/* a raster format for casting pre-computed
    per-pixel shadows and highlights

    This is a signed 8-bit format.
    Values >0 are highlight, <0 are shadow.
    A value of 0 has no effect.

    NOTE 1: Blitting with this format will fail
        if a BlitMode other than BLIT_COLORMOD
        is passed to RasterBlit
    NOTE 2: Maximum shadow/highlight is 50%
*/
#define FORMAT_FLAT_SHADE       2
/* the highest builtin format */
#define FORMAT_HIGHEST          2

/* the first user-defined format */
#define FORMAT_USER             0x0100

/* retrieves a format based on specified format ID
    WARNING: not thread-safe */
EXTERNC const RasterFormat __cdecl LookupFormat(unsigned int fmt);

/* copies specified format into an internally-maintained
    buffer and returns the index
    WARNING: not thread-safe */
EXTERNC unsigned int __cdecl RegisterUserFormat(RasterFormat *fmt);


/*
    helper for custom formats
    determines and calls the most optimal bilinear filter routine available
    pels is an array containing the four nearest pixels in ARGB format like so: {u0, u1, v0, v1}
    mods is an array of which factor to multiply them by like so: {(1-u)*(1-v), floor(u)*(1-v),(1-u)*floor(v),floor(u)*floor(v)}
    testmode indicates whether to test for colorkey or full transparency for better results for some pixels,
    and colorkey is the value to be returned if all supplied pixels fail this test (otherwise an average will fill in)
    the return value is the interpolated pixel
*/
#define BILINEAR_TEST_COLORKEY  1
#define BILINEAR_TEST_ALPHA     2
EXTERNC Pixel (__cdecl *InterpolateBilinear)(Pixel *pels, Half *mods, int testmode, Pixel colorkey);

#endif /* NJF_RASTER_FORMATS_H */
